<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title><c:choose><c:when test="${editMode}">编辑图书</c:when><c:otherwise>添加图书</c:otherwise></c:choose></title>
    <!-- 引入Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .msg {
            color: red;
            text-align: center;
            margin-bottom: 15px;
            font-weight: bold;
        }
    </style>
</head>

<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container-fluid">
        <a class="navbar-brand" href="#">图书馆管理系统 - <c:choose><c:when
                test="${editMode}">编辑图书</c:when><c:otherwise>添加图书</c:otherwise></c:choose></a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
                aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link" href="adminDashboardServlet">返回控制面板</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<div class="container mt-5">
    <c:if test="${msg!= null}">
        <p class="msg">${msg}</p>
    </c:if>
    <h2><c:choose><c:when test="${editMode}">编辑图书信息</c:when><c:otherwise>添加新图书</c:otherwise></c:choose></h2>
    <form id="bookForm"
          action="<c:choose><c:when test="${editMode}">updateBookServlet</c:when><c:otherwise>addBookServlet</c:otherwise></c:choose>"
          method="post">
        <c:if test="${editMode}">
            <input type="hidden" name="id" value="${book.id}"/>
        </c:if>
        <div class="form-group">
            <label for="name">图书名</label>
            <input type="text" class="form-control" id="name" name="name" value="${book.name}" required>
        </div>
        <div class="form-group">
            <label for="author">作者</label>
            <input type="text" class="form-control" id="author" name="author" value="${book.author}" required>
        </div>
        <div class="form-group">
            <label for="score">评分</label>
            <input type="number" step="0.1" class="form-control" id="score" name="score" value="${book.score}" required>
        </div>
        <div class="form-group">
            <label for="count">图书数量</label>
            <input type="number" class="form-control" id="count" name="count" value="${book.count}" required>
        </div>
        <div class="form-group">
            <label for="ISBN">ISBN</label>
            <input type="text" class="form-control" id="ISBN" name="ISBN" value="${book.ISBN}" required>
        </div>
        <div class="form-group">
            <label for="category">类别</label>
            <input type="text" class="form-control" id="category" name="category" value="${book.category}" required>
        </div>
        <button type="submit" class="btn btn-primary"><c:choose><c:when
                test="${editMode}">保存修改</c:when><c:otherwise>添加图书</c:otherwise></c:choose></button>
    </form>
</div>

<!-- 引入Bootstrap JS（用于导航栏折叠等交互功能） -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 前端表单验证（简单示例，可根据需求扩展）
    document.getElementById('bookForm').addEventListener('submit', function (e) {
        var bookName = document.getElementById('bookName').value;
        if (bookName === '') {
            alert('图书名不能为空');
            e.preventDefault();
        }
    });
</script>
</body>

</html>